package io.gitee.chearnee.fw.dingtalk;

import com.dingtalk.api.request.OapiRobotSendRequest.At;
import io.gitee.chearnee.fw.dingtalk.manager.DingTalkManager;
import io.gitee.chearnee.fw.dingtalk.msg.DingTalkBaseMessage;
import io.gitee.chearnee.fw.dingtalk.robot.DingTalkRobot;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2021/12/13 12:34
 */
@Component
@RequiredArgsConstructor
public class DingTalkManagerUtil {

  private final DingTalkManager dingTalkManager;
  @Value("${spring.profiles.active}")
  private String activeProfile;

  public final void sendMsg(String talkName, DingTalkBaseMessage dravenMessageTemplate, String... phone) {
    At at = new At();
    if (phone != null) {
      ArrayList<String> phones = new ArrayList<>(Arrays.asList(phone));
      at.setAtMobiles(phones);
    }
    String profile = activeProfile;
    dravenMessageTemplate.setTitle("【" + profile + "】-" + dravenMessageTemplate.getTitle());
    try (DingTalkRobot robot = dingTalkManager.getRobot(talkName)) {
      robot.sendMarkdown(dravenMessageTemplate.getTitle(), dravenMessageTemplate.toMessage(), at);
    }
  }
}
